*********************************************************************************************
*Replication code for Oliver, Steven, Ryan Jablonski, Justin Hastings. 
*"The Tortuga Disease: The Perverse Effects of Illicit Wealth" Forthcoming International Studies Quarterly (Accepted May 2016)
*Creates Tables in main draft and appendix
*Version 10 June, 2016
*********************************************************************************************

clear *
cd "<directory location>"

* First we generate the instrumental variable using ship characteristics
use RansomDataByShip.dta, clear
xi:reg ransom_amount_clean ship_tonnage ship_oil ship_yacht rich_flag i.month log_suezvessels log_monthcount gdp_growth monsoondummy pop_puntland ramadan 
predict double ransom_amount_predicted
fillin month year
*Collapse variable by year and month to get an instrumented estimate of monthly ransom amounts
collapse  (sum) ransom_amount_predicted, by(year month)
g log_ransom_amount_predicted = log(ransom_amount_predicted + 1)
*We set any months without ransoms (or which have a negative estimate) to zero
replace log_ransom_amount_predicted = 0 if missing(log_ransom_amount_predicted)
sort year month
save RansomInstrument.dta, replace

use RansomData.dta, clear
sort year month
*Merge in ransom instrument
merge year month using RansomInstrument.dta, replace update
sort region month2

 * TABLE 1
local control = "log_suezvessels log_monthcount monsoondummy gdp_growth  ramadan livestockban pmf"
local treat = "L_log_ransom_amount"
local treatDD1 = "i.region*log_ransom_amount"
local treatDD2 = "i.region*L_log_ransom_amount"

xi:prais   log_exchange `treatDD1' `control' i.month ,  robust
xi:prais   log_exchange `treatDD2' `control' i.month ,  robust
*save covariance matrix so we can create prediction figures
matrix cov_exchange = e(V)
matrix coef_exchange = e(b)
svmat cov_exchange
svmat coef_exchange
outsheet cov_* using "cov_exchange.csv", comma replace
outsheet coef_* using "coef_exchange.csv", comma replace
drop cov_* coef_*	
xi:prais    log_exchange `treat' `control' i.month if region==1,  robust
xi:prais    log_exchange `treat' `control' i.month if region==0,   robust	
ivregress 2sls log_exchange (L_log_ransom_amount=L.log_ransom_amount_predicted L.log_ship_tonnage  L.ship_oil L.ship_yacht L.rich_flag) `control' i.month L.log_exchange if region==1, robust


* TABLE 2 
local control = "log_suezvessels log_monthcount monsoondummy gdp_growth  ramadan livestockban pmf"
local treat = " log_ransom_amount"
local treatDD1 = "i.region*log_ransom_amount"
local treatDD2 = "i.region*L_log_ransom_amount"
xi:prais   log_exports `treatDD1' `control' i.month,  robust
*save covariance matrix so we can create prediction figures
matrix cov_exports = e(V)
matrix coef_exports = e(b)
svmat cov_exports
svmat coef_exports
outsheet cov_* using "cov_exports.csv", comma replace
outsheet coef_* using "coef_exports.csv", comma replace
drop cov_* coef_*	
xi:prais   log_exports `treatDD2' `control' i.month ,  robust
xi:prais    log_exports `treat' `control' i.month if region==1 ,  robust
xi:prais    log_exports `treat' `control' i.month if region==0 ,   robust	
ivregress 2sls log_exports (log_ransom_amount=log_ransom_amount_predicted log_ship_tonnage  ship_oil ship_yacht rich_flag) `control' i.month L.log_exchange if region==1 , robust


* TABLE 3 
local control = "log_suezvessels log_monthcount monsoondummy gdp_growth  ramadan livestockban pmf"
local treat = " log_ransom_amount"
local treatDD1 = "i.region*log_ransom_amount"
local treatDD2 = "i.region*L_log_ransom_amount"
xi:prais   log_imports `treatDD1' `control' i.month ,  robust
*save covariance matrix so we can create prediction figures
matrix cov_imports = e(V)
matrix coef_imports = e(b)
svmat cov_imports
svmat coef_imports
outsheet cov_* using "cov_imports.csv", comma replace
outsheet coef_* using "coef_imports.csv", comma replace
drop cov_* coef_*	
xi:prais   log_imports `treatDD2' `control' i.month ,  robust
xi:prais    log_imports `treat' `control' i.month if region==1,  robust
xi:prais    log_imports `treat' `control' i.month if region==0,   robust	
ivregress 2sls log_imports (log_ransom_amount=log_ransom_amount_predicted log_ship_tonnage  ship_oil ship_yacht rich_flag) `control' i.month L.log_imports if region==1, robust
	
* TABLE A1
* Volumes
local control = "log_monthcount log_suezvessels   monsoondummy gdp_growth  ramadan livestockban pmf"
local treat = " log_ransom_amount"
local treatDD1 = "i.region*log_ransom_amount"
xi:prais   log_imports_volume `treatDD1' `control' i.month ,  robust
xi:prais   log_exports_volume `treatDD1' `control' i.month ,  robust

	
* TABLE A2a
* Control Variables 1
local control = "log_monthcount log_suezvessels   monsoondummy gdp_growth  ramadan livestockban pmf log_forcecount log_acledcount"
local treat1 = " log_ransom_amount"
local treat2 = " L_log_ransom_amount"
xi:prais   log_exchange `treat2' `control' i.month if region==1  ,  robust
xi:prais   log_exports `treat1' `control' i.month if region==1 ,  robust
xi:prais   log_imports `treat1' `control' i.month if region==1,  robust

* TABLE A2b
* Control Variables 2
local control = "log_monthcount log_suezvessels   monsoondummy gdp_growth  ramadan livestockban pmf puntlandcrisis log_fdi_somalia log_aid_somalia"
local treat1 = " log_ransom_amount"
local treat2 = " L_log_ransom_amount"
xi:prais   log_exchange `treat2' `control' i.month if region==1  ,  robust
xi:prais   log_exports `treat1' `control' i.month if region==1 ,  robust
xi:prais   log_imports `treat1' `control' i.month if region==1,  robust

* TABLE A2c
* Control Variables 3
local control = "log_monthcount log_suezvessels   monsoondummy gdp_growth  ramadan livestockban pmf  wgi"
local treat1 = " log_ransom_amount"
local treat2 = " L_log_ransom_amount"
xi:prais   log_exchange `treat2' `control' i.month if region==1  ,  robust
xi:prais   log_exports `treat1' `control' i.month if region==1 ,  robust
xi:prais   log_imports `treat1' `control' i.month if region==1,  robust

* TABLE A3
* Alternate Lag Structures
local control = "log_monthcount log_suezvessels   monsoondummy gdp_growth  ramadan livestockban pmf"
local treat1 = " log_ransom_amount"
local treat2 = "log_ransom_amount L_log_ransom_amount"
local treat3 = "log_ransom_amount L_log_ransom_amount L2_log_ransom_amount"
local treat4 = "log_ransom_amount L_log_ransom_amount L2_log_ransom_amount L3_log_ransom_amount"
xi:prais   log_exchange `treat1' `control' i.month if region==1,  robust
xi:prais   log_exchange `treat2' `control' i.month if region==1,  robust
xi:prais   log_exchange `treat3' `control' i.month if region==1,  robust
xi:prais   log_exchange `treat4' `control' i.month if region==1,  robust
xi:prais   log_exports `treat1' `control' i.month if region==1,  robust
xi:prais   log_exports `treat2' `control' i.month if region==1,  robust
xi:prais   log_exports `treat3' `control' i.month if region==1,  robust
xi:prais   log_exports `treat4' `control' i.month if region==1,  robust
xi:prais   log_imports`treat1' `control' i.month if region==1,  robust
xi:prais   log_imports `treat2' `control' i.month if region==1,  robust
xi:prais   log_imports `treat3' `control' i.month if region==1,  robust
xi:prais   log_imports `treat4' `control' i.month if region==1,  robust
	
* TABLE A4
* First Differences
g D2log_ransom_amount=L1.log_ransom_amount-L2.log_ransom_amount
g D1log_ransom_amount=log_ransom_amount-L1.log_ransom_amount
local control = "D.log_monthcount D.log_suezvessels   D.monsoondummy D.gdp_growth  D.ramadan D.livestockban D.pmf"
local treat = " D2log_ransom_amount"
local treat2 = " D1log_ransom_amount"
xi:reg   D.log_exchange `treat' `control' i.month if region==1,  robust
xi:reg   D.log_exports  `treat2'   `control' i.month if region==1,  robust
xi:reg   D.log_imports  `treat2' `control' i.month  if region==1, robust 

* TABLE A5
* Dropping pre-2008
local control = "log_monthcount log_suezvessels   monsoondummy gdp_growth  ramadan livestockban pmf "
local treat1 = " log_ransom_amount"
local treat2 = " L_log_ransom_amount"
xi:prais   log_exchange `treat2' `control' i.month if region==1 & year>2007 ,  robust
xi:prais   log_exports `treat1' `control' i.month if region==1 & year>2007,  robust
xi:prais   log_imports `treat1' `control' i.month if region==1 & year>2007,  robust

* TABLE A6
* Migrant Capital
local control = "log_monthcount log_suezvessels   monsoondummy gdp_growth  ramadan livestockban pmf log_migrant_capital"
local treat1 = " log_ransom_amount"
local treat2 = " L_log_ransom_amount"
xi:prais   log_exchange `treat2' `control' i.month if region==1  ,  robust
xi:prais   log_exports `treat1' `control' i.month if region==1 ,  robust
xi:prais   log_imports `treat1' `control' i.month if region==1 ,  robust

	




	